<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\tinyinfo\model;
use think\Cache;
use mip\Paginationm;
use think\Db;
use think\Controller;
use think\Model;

class Tinyinfo extends Model
{
    /**
     * 获取一条广告，如果不存在，则创建这条记录
     * @param $ggcode
     * @param int $index
     * @return array
     */
    public function getOne($where,$order,$index){
        $cacheKey = md5( 'c_tinyinfo_getOne_'.(is_array($where)?implode('_',array_keys($where) ).'_'.implode('_',$where):$where).'_'.$order.'_'.$index );
        $gg = Cache::get($cacheKey);
        if($gg){
            return $gg;
        }

        $gg = db('tinyinfo')->where($where)->order($order)->page($index,1)->find();
        $gg['htmlDescription'] = encodeHtml($gg['description']);

        Cache::set($cacheKey,$gg,60*60);
        Cache::tag('tinyinfo',$cacheKey);
        return $gg;
    }

    /**
     * 获取若干条广告，如果不存在，则新增记录
     * @param $ggcode
     * @param int $cnt
     * @return array
     */
    public function getList($where,$order,$page=1,$limit=100){
        $cacheKey = md5('c_tinyinfo_getList_'.(is_array($where)?implode('_',array_keys($where) ).'_'.implode('_',$where):$where).'_'.$order.'_'.$page.'_'.$limit );
        $gg = Cache::get($cacheKey);
        if($gg){
            return $gg;
        }
        $gg = db('tinyinfo')->where($where)->order($order)->page($page,$limit)->select();

        foreach ($gg as $k=>$v){
            $v['htmlDescription'] = encodeHtml($v['description']);
            $gg[$k] = $v;
        }

        Cache::set($cacheKey,$gg,60*60);
        Cache::tag('tinyinfo',$cacheKey);
        return $gg;
    }

    public function getCount($where){
        $cacheKey = md5('c_tinyinfo_getCount_'.(is_array($where)?implode('_',array_keys($where) ).'_'.implode('_',$where):$where) );
        $gg = Cache::get($cacheKey);
        if($gg){
            return $gg;
        }

        $gg = db('tinyinfo')->where($where)->field('1')->count();
        Cache::set($cacheKey,$gg,60*60);
        Cache::tag('tinyinfo',$cacheKey);
        return $gg;
    }

    public function getById($id){
        $cacheKey = 'c_tinyinfo_geById_'.$id;
        $gg = Cache::get($cacheKey);
        if($gg){
            return $gg;
        }

        $gg = db('tinyinfo')->cache('c_tinyinfo_geById_'.$id,60*60,'tinyinfo')->where('id',$id)->find();
        $gg['htmlDescription'] = encodeHtml($gg['description']);

        Cache::set($cacheKey,$gg,60*60);
        Cache::tag('tinyinfo',$cacheKey);
        return $gg;
    }

    public function doDelById($id){
        $res = db('tinyinfo')->where('id',$id)->delete();
        Cache::clear('tinyinfo');
        return $res;
    }

    public function doAdd($arr){
        $res = db('tinyinfo')->insert($arr);
        Cache::clear('tinyinfo');
        return $res;
    }
    public function doEdit($id,$arr){
        $res = db('tinyinfo')->where('id',$id)->update($arr);
        Cache::clear('tinyinfo');
        return $res;
    }


}